package com.zim.access.webapi.session;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.zim.access.common.chat.Session;
import com.zim.access.common.page.Page;
import com.zim.access.common.page.QueryOptions;
import com.zim.access.service.session.SessionService;
import com.zim.access.domain.session.SessionDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "session", description = "会话接口")
@RestController
@RequestMapping(value = "/session")
public class SessionController {

    /**
     * 注入session 服务类
     */
    private final SessionService sessionService;

    @Autowired
    public SessionController(SessionService sessionService) {
        this.sessionService = sessionService;
    }

    @ApiOperation("单个会话查询")
    @GetMapping("/{id}")
    @ResponseBody
    public Session findById(@PathVariable String id) {
        return sessionService.findById(id);
    }

    @ApiOperation("会话列表")
    @GetMapping
    @ResponseBody
    public Page<Session> findMay(QueryOptions options, Wrapper<SessionDo> wrapper) {
        return sessionService.findMay(options, wrapper);
    }

    @ApiOperation("根据用户ID查询会话列表")
    @GetMapping("/search/{userId}")
    @ResponseBody
    public Page<Session> searchByUserId(@PathVariable String userId, QueryOptions options) {
        return sessionService.searchByUserId(userId, options);
    }

    @ApiOperation("根据用户名称查询会话列表")
    @GetMapping("/search")
    @ResponseBody
    public Page<Session> searchByUserName(@RequestParam String userName, QueryOptions options) {
        return sessionService.searchByUserName(userName, options);
    }


    @ApiOperation("创建会话")
    @PostMapping
    @ResponseBody
    public Session createSession(@RequestBody Session session) {
        return sessionService.createSession(session);
    }

    @ApiOperation("删除会话")
    @DeleteMapping("/{id}")
    @ResponseBody
    public boolean deleteSession(@PathVariable String id) {
        return sessionService.deleteSession(id);
    }

    @ApiOperation("更新会话")
    @PatchMapping("/{id}")
    @ResponseBody
    public boolean updateSession(@PathVariable String id, @RequestBody Session session) {
        return sessionService.updateSession(id, session);
    }

    @ApiOperation("重置会话未读")
    @DeleteMapping
    @ResponseBody
    public boolean resetSession() {
        return false;
    }

    @ApiOperation("查询单个会话未读")
    @GetMapping("/search/unread/{id}")
    @ResponseBody
    public Long searchUnReadById(@PathVariable String id) {
        return sessionService.searchUnReadById(id);
    }

    @ApiOperation("查询所有会话未读")
    @GetMapping("/search/unread/all")
    @ResponseBody
    public Long searchAllUnRead() {
        return sessionService.searchAllUnRead();
    }
}
